﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using TreeSharp;

using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;

namespace FelMaster.Classes.Deathknight
{
    public class UnholyPlague : TalentBase
    {
        public override string Name
        {
            get { return "Unholy v1.62"; }
        }

        public override string KeySpell
        {
            get { return "Scourge Strike"; }
        }

        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                    new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                    SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                    SC.CastSpell("Berserking", a => true, "Berserking"),
                    SC.CastSpell("Unholy Frenzy", a => !(SC.PlayerHasBuff("Time Warp") || SC.PlayerHasBuff("Bloodlust") || SC.PlayerHasBuff("Heroism") || SC.PlayerHasBuff("Ancient Hysteria")), "Unholy Frenzy"),
                    SC.UseTrinket(69113, a => SC.PlayerCountBuff("Titanic Power") == 5, "Popping Apparatus of Khaz'goroth!"),
                    SC.UseTrinket(68972, a => SC.PlayerCountBuff("Titanic Power") == 5, "Popping Apparatus of Khaz'goroth!"),
                    SC.CastSpell("Outbreak", a => SC.TargetDebuffTimeLeft("Blood Plague") < 2 || SC.TargetDebuffTimeLeft("Frost Fever") < 2, "Outbreak"),
                    SC.CastSpell("Icy Touch", a => SC.TargetDebuffTimeLeft("Frost Fever") < 2, "Icy Touch"),
                    SC.CastSpell("Plague Strike", a => SC.TargetDebuffTimeLeft("Blood Plague") < 2, "Plague Strike"),
                    SC.CastSpell("Dark Transformation", a => SC.IsUsableSpell("Dark Transformation"), "Dark Transformation"),
                    SC.CastSpell("Summon Gargoyle", a => true, "Summon Gargoyle"),
                    new Decorator(
                        ret => Me.GotTarget && Me.UnholyRuneCount == 2 && Me.CurrentRunicPower < 110 && SC.CanCast("Death and Decay") && !Me.CurrentTarget.Name.Contains("Ultraxion"),
                        new Sequence(
                            new TreeSharp.Action(ret => SC.CastSpell("Death and Decay")),
                            new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(WoWMathHelper.CalculatePointFrom(Me.Location, Me.CurrentTarget.Location,Math.Max(0, Me.CombatReach+0.33333f + Me.CurrentTarget.CombatReach) - 3f)))
                        )
                    ),
                    SC.CastSpell("Scourge Strike", a => Me.UnholyRuneCount == 2 && Me.CurrentRunicPower < 110, "Scourge Strike"),
                    SC.CastSpell("Festering Strike", a => Me.BloodRuneCount == 2 && Me.FrostRuneCount == 2 && Me.CurrentRunicPower < 110, "Festering Strike"),
                    SC.CastSpell("Death Coil", a => SC.PlayerHasBuff("Sudden Doom") || Me.CurrentRunicPower > 90, "Death Coil"),
                    new Decorator(
                        ret => Me.GotTarget && SC.CanCast("Death and Decay") && !Me.CurrentTarget.Name.Contains("Ultraxion"),
                        new Sequence(
                            new TreeSharp.Action(ret => SC.CastSpell("Death and Decay")),
                            new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(WoWMathHelper.CalculatePointFrom(Me.Location, Me.CurrentTarget.Location,Math.Max(0, Me.CombatReach+0.33333f + Me.CurrentTarget.CombatReach) - 3f)))
                        )
                    ),
                    SC.CastSpell("Pestilence", a => SC.CountEnnemiesInRange(Me.CurrentTarget.Location, 10) > 1 && SC.TargetHasDebuff("Blood Plague") && SC.TargetHasDebuff("Frost Fever") && SC.SpellCooldown("Outbreak") > 58, "Pestilence"),
                    SC.CastSpell("Scourge Strike", a => true, "Scourge Strike"),
                    SC.CastSpell("Festering Strike", a => true, "Festering Strike"),
                    SC.CastSpell("Death Coil", a => true, "Death Coil"),
                    SC.CastSpell("Blood Tap", a => Me.BloodRuneCount > 0, "Death Coil"),
                    SC.CastSpell("Empower Rune Weapon", a => Me.UnholyRuneCount == 0 && Me.DeathRuneCount == 0, "Empower Rune Weapon"),
                    SC.CastSpell("Horn of Winter", a => true, "Horn of Winter")
                );
            }
        }
        
        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastSpell("Raise Dead", a => !Me.Mounted && (Me.Pet == null || Me.Pet.Dead), "Raise Dead")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}
